const userService = require('../service/user');
const retCode = require('../utils/retcode')
class UserController {
  async profile(ctx) {
    const res = await userService.profile();
    ctx.body = res.data;
  }

  /**
   * 登录
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */
  async login ( ctx ) {
    let form = ctx.request.body
    
    const args = {
        username: form.username,
        userpass: form.password
    }
        
    let result = {
        code: retCode.Success,    
        data: null
    }
    
    //验证非空
    if(!args.username || !args.userpass){     
        ctx.body = {
          status: retCode.ArgsError,
          statusText: '参数错误',
          currentAuthority: 'guest',
        };
    } else {
      //根据用户名得到用户信息
      let userResult = await userService.getByUserName(args)

      //用户不存在
      if(userResult.data.length == 0){
     
          ctx.body = {
            status: retCode.UserNotExist ,
            statusText: '用户不存在',
            currentAuthority: 'guest',
          };
      } else if (userResult.data[0].UserName != args.username || userResult.data[0].UserPass != args.userpass){  //用户名或密码错误     
          ctx.body = {
            status: retCode.UsernameOrPasswordError ,
            statusText: '用户名或者密码错误',
            currentAuthority: 'guest',
          };

      }  else {

        //将用户ID存入Session中
        ctx.session = {id: userResult.data[0].Id}

        ctx.body = {
          status: retCode.Success,
          statusText: '成功',
          currentAuthority: userResult.data[0].Id,
        };
      }
    }
  }

   /**
   * 注册
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */
  async register ( ctx ) {
    let form = ctx.request.body
    
    const args = {
        username: form.name,
        userpass: form.passwd
    }
        
    let result = {
        code: retCode.Success,    
        data: null
    }
    
    //验证非空
    if(!args.username || !args.userpass){      
        ctx.body = {
          status: retCode.ArgsError  ,
          statusText: '参数错误',
          currentAuthority: '0',
        };
    }else{
      //根据用户名得到用户数量
      let userNumResult = await userService.getCountByUserName(args)
      //用户名已被注册
      if(userNumResult.data[0].UserNum > 0){      
        ctx.body = {
          status: retCode.UserExisted  ,
          statusText: '用户已经存在',
          currentAuthority: '0',
        };
      }else{
        //插入注册数据
        let userResult = await userService.add(args)
        if(userResult.insertId <= 0){       
            ctx.body = {
              status: retCode.Fail  ,
              statusText: '插入注册数据失败',
              currentAuthority: '0',
            };
        }else{
          ctx.body = {
            status: retCode.Success,
            statusText: '插入注册数据成功',
            currentAuthority: 'user',
          };
        }
      }
    }
  }


  async logout(ctx) {
    ctx.body = {
      status: 200,
      statusText: 'ok',
      currentAuthority: 'guest',
    };
  }

   /**
   * 分页查询
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */

  async userFilter(ctx) {
    let form = ctx.request.body
    const args = {
      username: form.username,
    }

    //用户信息List
    let userListResult = await userService.getListByFilter({
      where: args,
      limit: ctx.pagination.limit,
      offset: ctx.pagination.offset
    })

    let userNumResult = await userService.getCountByFilter(args)

    ctx.body = {
      status: 200,
      statusText: 'ok',
      data:userListResult
    }
    
 
    // This is needed in order to expose the length in `Content-Range` header.
    ctx.pagination.length = userNumResult[0].total
   

  }
  async mockdata(ctx){
    ctx.body =  {
      success: true,
      message: "T1108泰福高正品不锈钢真空保温壶保温瓶水壶暖",
      data: {
        foo: "DJ美国百丽耐热"
      }
    };
  }
  async yearFilter(ctx) {
    let form = ctx.request.body
    let pageIndex = parseInt(form.page)
    let limit = parseInt(form.pageSize)
    let offset = (pageIndex -1)* limit
    const args = {
      ymonth: form.ymonth,
      devicetype: form.devicetype,
      limit: limit,
      offset: offset
    }

    //List
    let data = await userService.getYearListByFilter(args)

    let totalResult = await userService.getYearCountByFilter(args)
    let total = totalResult[0].total
    ctx.body = {
      status: retCode.Success,
      statusText: '查询成功',
      data:{
        table:data,
        pagination: {
          pageIndex: pageIndex,
          recordTotal: total,
          pageSize: limit,
        },
      },
    }

  }
}

module.exports = new UserController();
